use std::thread::spawn;

use tauri::WebviewUrl;

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn create_window(handle: tauri::AppHandle, label: String, title: String, path: String) {
    spawn(move || {
        let result = tauri::WebviewWindowBuilder::new(&handle, label, WebviewUrl::App(path.into()))
            .decorations(false)
            .devtools(true)
            .drag_and_drop(false)
            .title(title)
            .build();
        match result {
            Ok(_window) => {
                // _window.request_user_attention(Some(tauri::UserAttentionType::Informational));
                println!("created!");
            }
            Err(e) => {
                println!("{e:?}");
            }
        }
    });
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_log::Builder::new().build())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![greet, create_window])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
